﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NopSolutions.NopCommerce.BusinessLogic;
using NopSolutions.NopCommerce.BusinessLogic.Audit;
using NopSolutions.NopCommerce.BusinessLogic.Categories;
using NopSolutions.NopCommerce.BusinessLogic.Configuration.Settings;
using NopSolutions.NopCommerce.BusinessLogic.Directory;
using NopSolutions.NopCommerce.BusinessLogic.Localization;
using NopSolutions.NopCommerce.BusinessLogic.Manufacturers;
using NopSolutions.NopCommerce.BusinessLogic.Products;
using NopSolutions.NopCommerce.Common;
using NopSolutions.NopCommerce.Common.Utils;
using NopSolutions.NopCommerce.LuceneHelper;

namespace NopSolutions.NopCommerce.Web.Templates.Lucene
{
    public partial class LuceneSearch : BaseNopUserControl
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (!String.IsNullOrEmpty(this.SearchTerms))
                    txtSearchTerm.Text = this.SearchTerms;
                BindData();
            }
        }
       
        protected void BindData()
        {
            try
            {
                string keywords = txtSearchTerm.Text.Trim();

                if (!String.IsNullOrEmpty(keywords))
                {
                    int searchTermMinimumLength = SettingManager.GetSettingValueInteger("Search.ProductSearchTermMinimumLength", 3);
                    if (keywords.Length < searchTermMinimumLength)
                        throw new NopException(string.Format(LocalizationManager.GetLocaleResourceString("Search.SearchTermMinimumLengthIsNCharacters"), searchTermMinimumLength));
                    
                    int customerId = 0;
                    if (NopContext.Current.User != null)
                        customerId = NopContext.Current.User.CustomerId;
                    SearchLogManager.InsertSearchLog(txtSearchTerm.Text, customerId, DateTime.UtcNow);
                }
                else
                {
                    ctrlLuceneProducts.Visible = false;
                }
            }
            catch (Exception exc)
            {
                ctrlLuceneProducts.Visible = false;
                lblError.Text = Server.HtmlEncode(exc.Message);
            }
        }

        protected void lvProducts_OnPagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
        {
            //this.pagerProducts.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
            BindData();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            var filterContext = new FilterContext();
           
            filterContext.KeyWords = txtSearchTerm.Text.Trim();
            
            filterContext.RedirectToFilteredPage();
        }

        protected override void OnPreRender(EventArgs e)
        {
            BindJQuery();

            //this.cbAdvancedSearch.Attributes.Add("onclick", "toggleAdvancedSearch();");
            this.txtSearchTerm.Attributes.Add("onkeydown", "if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('" + btnSearch.ClientID + "').click();return false;}} else {return true}; ");

            lblNoResults.Visible = !ctrlLuceneProducts.Visible;

            base.OnPreRender(e);
        }

        public string SearchTerms
        {
            get
            {
                return CommonHelper.QueryString("SearchTerms");
            }
        }
    
    }
}